<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Nguyễn Thanh Hải - Team 2
//Module 	: SideBarManager
//Chức năng	: Quản lý các module trên sidebar

class Admin_ModuleController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
	}
	
	public function indexAction(){
		
		//Start ------- Check Input Parameter
		//End ------- Check Input Parameter
		
		//Start ------- Khởi tạo Parameter
		$path = APPLICATION_PATH."/widgets";
		$tree = dir($path);
		$count = 0;
		$tbSidebarModule = new Admin_Model_SidebarModule();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Vòng lặp đọc folder widgets
		while($dir = $tree->Read()){
			if (is_dir($path."/".$dir)&&($dir!=".")&&($dir!="..")&&($dir!=".svn"))
			{
				$xmlpath = $path."/".$dir."/";
				$xmlconfig = new SimpleXMLElement($xmlpath."config.xml",null,true);//Read config.xml
				if($xmlconfig->type=="sidebar")
				{
					$module = $tbSidebarModule->selectModuleByCode($xmlconfig->name);//Check Exist in DB
					if(count($module)>0) 
					{
						$haved = $module[0]["sort_order"];
					}
					else 
					{
						$haved = -1;
					}
					
					$user = new User();
					$user->init();
					$user->check_login();
					$hasmodifywidget=$user->modifiy_widget($xmlconfig->name);
					if($hasmodifywidget=="true")
					{
						$widget  = array(
							"module_code" => $xmlconfig->name,
							"module_type" => $xmlconfig->type,
							"module_title" => $xmlconfig->titleDefault,
							"module_sort" => $haved,		
						);
						$widgets[] = $widget;
					}
				}
			}
		}	
		//End ------- Vòng lặp đọc folder widgets
		
		//Start ------- Truyền Parameter qua View
		$this->view->installUrl = "/admin/installwidget/sidebar";
		$this->view->unInstallUrl = "/admin/uninstallwidget/sidebar";
		$this->view->updateUrl = "/admin/installwidget/updatemodule";
		$this->view->widgets = $widgets;
		//End ------- Truyền Parameter qua View
	}
}